Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports C4F.DevKit.Contacts
Imports C4F.DevKit.Contacts.Controls

Namespace C4F.DevKit.Contacts.Samples.Win
	Public Partial Class MainForm
		Inherits Form
		#Region "Constructor"

		Public Sub New()
			InitializeComponent()

			AddHandler contactsListView1.ContactDoubleClick, AddressOf contactsListView1_ContactDoubleClick
		End Sub

		#End Region ' Constructor

		#Region "Methods"

		#Region "Event Handlers"

		Private Sub mnuNewContact_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuNewContact.Click
			OnNewContactDetail()
		End Sub

		Private Sub mnuUpdateContact_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuUpdateContact.Click
			OnUpdateContactDetail(contactsListView1.SelectedContactID)
		End Sub

		Private Sub tbtnNewContact_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tbtnNewContact.Click
			OnNewContactDetail()
		End Sub

		Private Sub contactsListView1_ContactDoubleClick(ByVal sender As Object, ByVal e As ContactDoubleClickArgs)
			OnUpdateContactDetail(e.ContactID)
		End Sub

		Private Sub tbtnRefreshContacts_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tbtnRefreshContacts.Click
			RefreshContacts()
		End Sub

		Private Sub mnuImportFromDatabase_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuImportFromDatabase.Click
			Try
				OnImportFromDatabase()
			Catch ex As Exception
				DisplayException(ex)
			End Try
		End Sub

		Private Sub mnuExit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuExit.Click
			Application.Exit()
		End Sub

		Private Sub mnuImportFromVCard_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuImportFromVCard.Click
			Try
				OnImportFromVCard()
			Catch ex As Exception
				DisplayException(ex)
			End Try
		End Sub

		#End Region ' Event Handlers

		#Region "Private"

		Private Sub DisplayException(ByVal ex As Exception)
			If Not ex Is Nothing Then
				MessageBox.Show(ex.ToString())
			End If
		End Sub

		Private Sub OnUpdateContactDetail(ByVal contactID As String)
			Dim disp As ContactDetailDisplay = New ContactDetailDisplay(contactID)
			disp.ShowDialog()
			RefreshContacts()
		End Sub

		Private Sub OnNewContactDetail()
			Dim disp As ContactDetailDisplay = New ContactDetailDisplay()
			disp.ShowDialog()
			RefreshContacts()
		End Sub

		Private Sub RefreshContacts()
			contactsListView1.RefreshList()
		End Sub

		Private Sub OnImportFromDatabase()
			Dim frm As ContactDBList = New ContactDBList()

			If frm.ShowDialog() = System.Windows.Forms.DialogResult.OK AndAlso frm.ContactID <> Guid.Empty Then
				contactsService1.ImportContactFromDatabase(frm.ContactID)
				RefreshContacts()
			End If
		End Sub

		Private Sub OnImportFromVCard()
			contactsService1.ImportContactFromVCard()
			contactsListView1.RefreshList()
		End Sub

		#End Region ' Private

		#End Region ' Methods
	End Class
End Namespace